import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext.webapp import template

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainHandler(webapp.RequestHandler):
    def get(self):
        name = self.request.get('name')
	template_values = {'name' : name}
    	path = os.path.join(os.path.dirname(__file__), 'hello.html')
        self.response.out.write(template.render(path, template_values))

class GoodbyeHandler(webapp.RequestHandler):
  def get(self):
    self.response.out.write('Goodbye app engine world!')

class DiceHandler(webapp.RequestHandler):
  def get(self):
    num_times = self.request.get('times')
    if num_times.isdigit() == True:
        num_times = int(num_times)
    	#! /usr/bin/python
    	# A Python script that simulates rolling two dice a few times.
   	 
  	# Module for dealing with random numbers.
	import random
	
	
	# Number of times to roll the dice.
	DICE_ROLLS = 0
	repeat = True
	self.response.out.write("Rolling the dice " + str(num_times) + " times <br>")

    	def RollDice(num_sides):
	   x = random.randint(1,num_sides)
	   return x

   	while repeat == True and DICE_ROLLS != num_times:
           # Simulate rolling two dice.
	   dice_1 = RollDice(6)
	   dice_2 = RollDice(6)
	   self.response.out.write("Rolls: "+str(dice_1)+", "+str(dice_2) +" <br>") 
   	   DICE_ROLLS += 1
	   #print if snake eyes 	
	   if dice_1 == dice_2 == 1:
		self.response.out.write("Got snake eyes!<br>")
	   if dice_1 == dice_2 == 6:
	 	self.response.out.write("Got double sixes!<br>")
		repeat = False
                self.response.out.write("Rolled the dice "+str(DICE_ROLLS)+" times<br>")
    else:
        self.response.out.write('ERROR incorrect input type! (integers only)<br>')
    


def main():
  application = webapp.WSGIApplication([('/hello.*', MainHandler),
                                        ('/goodbye.*', GoodbyeHandler),
                                        ('/dice.*', DiceHandler)],
                                       debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
